/*
 * Copyright (c) 2012-2016, b3log.org & hacpai.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */
package com.ld.admin.service.webbot;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.ld.shieldsb.common.core.util.JsoupUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 魔力机器人 <a href="http://www.itpk.cn">ITPK</a> bot query service.
 *
 * @author <a href="http://relyn.cn">Relyn</a>
 * @version 1.0.0.0, Aug 8, 2016
 * @since 2.0.1
 */
@Slf4j
public class ItpkQueryService {

    /**
     * Chat with ITPK Robot.
     *
     * @param msg
     *            the specified message
     * @return robot returned message, return {@code null} if not found
     */
    public String chat(String msg) {
        if (StringUtils.isBlank(msg)) {
            return null;
        }

        if (StringUtils.isBlank(msg)) {
            return null;
        }

        try {

            /**
             * ITPK Robot URL.
             */
            String itpkApi = WebBotConfig.getString("itpk.api");
            /**
             * ITPK Robot API.
             */
            String itpkKey = WebBotConfig.getString("itpk.key");
            /**
             * ITPK Robot Key.
             */
            String itpkSecret = WebBotConfig.getString("itpk.secret");
            /*   接入URL：http://i.itpk.cn/api.php
                           接入参数：
                                   参数                      描述                                                                                                                                                                              是否必选
               question    对API的提问字符                                                                                                                                                  是
               limit       API最大分析次数，取值范围为2-8，值越大精确率越高，但是会影响API响应时间     否
               api_key     用户的Api Key                                                         否
               api_secret  用户的Api Secret                                                       否
               type        定义返回结果类型（普通文本/xml/json），暂未实现，默认普通文本                                 否
                           友情提示：不久之后api_key和api_secret这两个参数可能会变为必须携带的参数，为了更好的体验茉莉机器人，请您务必携带！*/

            Map<String, String> parms = new HashMap<>();
            parms.put("api_key", itpkKey);
            parms.put("api_secret", itpkSecret);
            parms.put("limit", "8");
            parms.put("question", msg);
            String responseContent = JsoupUtil.getRemoteURLDocument(itpkApi, 10 * 1000, parms).body().html(); // 可以同时设置超时时间

            return responseContent;
        } catch (final Exception e) {
            log.error("Chat with ITPK Robot failed", e);
        }

        return null;
    }
}
